package com.aliyun.oss.service.impl;

import com.aliyun.oss.mapper.SetMealMapper;
import com.aliyun.oss.pojo.DTO.SetMealDTO;
import com.aliyun.oss.pojo.Entity.SetMeal;
import com.aliyun.oss.pojo.Vo.FindByIdSetMealVO;
import com.aliyun.oss.pojo.Vo.SetMealVO;
import com.aliyun.oss.service.SetMealService;
import com.aliyun.oss.utils.AliyunOssOperator;
import com.aliyun.oss.utils.GetNowTime;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, SetMeal> implements SetMealService {
    @Autowired
    SetMealMapper setMealMapper;
    @Autowired
    AliyunOssOperator aliyunOssOperator;

    @Override
    public List<SetMealVO> getAllSetmeal() {
        List<SetMealVO> setMealVOList = setMealMapper.getAllSetmeal();
        return setMealVOList;
    }

    @Override
    public List<SetMealVO> querySetMealLikeByNameOrCategoryIdOrStatus(String name, Integer categoryId, Integer status) {

        List<SetMealVO> setMealVOList = setMealMapper.querySetMealLikeByNameOrCategoryIdOrStatus(name, categoryId, status);
        return setMealVOList;
    }

    @Override
    public void BatchDeleteSetMealsByIds(String[] ids) {
//        通过mybasticplus根据ids删除套餐
        setMealMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public void updateSetMealStatusById(Integer operateId, Integer id, Integer status) {
        LocalDateTime nowTime = GetNowTime.getNowTime();
        setMealMapper.update(new SetMeal(status, nowTime, operateId), new QueryWrapper<SetMeal>().eq("id", id));
    }

    @Override
    public void addSetMeal(Integer operateId, SetMealDTO setMealDTO) {
        LocalDateTime nowTime = GetNowTime.getNowTime();
//        对setMealDTO的照片调用阿里云上传图片，获取图片的url，注意判断是否为空
        String imageUrl = null;
        MultipartFile imageFile = setMealDTO.getImage();
        if (imageFile != null && !imageFile.isEmpty()) {
            try {
                byte[] content = imageFile.getBytes();
                String originalFilename = imageFile.getOriginalFilename(); // 安全调用
                // 调用阿里云 OSS 上传图片
                imageUrl = aliyunOssOperator.upload(content, imageFile.getOriginalFilename()); // OSS上传图片返回url
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        setMealMapper.insert(new SetMeal(setMealDTO.getName(), setMealDTO.getCategoryId(), setMealDTO.getPrice(), setMealDTO.getDescription(), imageUrl, nowTime,nowTime, operateId,operateId));
    }

    @Override
    public FindByIdSetMealVO getSetMealById(Integer id) {
//        根据id返回
        SetMeal setMeal = setMealMapper.selectById(id);
        return new FindByIdSetMealVO(setMeal.getId(), setMeal.getName(), setMeal.getCategoryId(), setMeal.getPrice(),setMeal.getImage(), setMeal.getDescription());
    }

    @Override
    public void updateSetMealById(Integer operateId, SetMealDTO setMealDTO) throws ClientException {
        LocalDateTime nowTime = GetNowTime.getNowTime();
//        通过id获取旧图片地址
        String oldImageUrl = setMealMapper.selectById(setMealDTO.getId()).getImage();
        if(oldImageUrl != null){
            aliyunOssOperator.deleteByUrl(oldImageUrl);
        }
//        先上传图片
        byte[] content = null;
        String originalFilename = null;
        String imageUrl = null;
        MultipartFile imageFile = setMealDTO.getImage();
        if (imageFile != null && !imageFile.isEmpty()) {
            try {
                content = imageFile.getBytes();
                originalFilename = imageFile.getOriginalFilename(); // 安全调用
                // 调用阿里云 OSS 上传图片
                imageUrl = aliyunOssOperator.upload(content, originalFilename);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
//        通过id更新套餐数据,实体类型是setMeal
        SetMeal setMeal=new SetMeal(setMealDTO.getId(),setMealDTO.getCategoryId(),setMealDTO.getName(),setMealDTO.getPrice(),setMealDTO.getDescription(),imageUrl,nowTime,nowTime,operateId,operateId);
        setMealMapper.updateById(setMeal);



    }


}
